package com.adobe.catalog.CustomComponent
{
	import Skin.Border.CustomBorderImageSkin;
	
	import flash.events.MouseEvent;
	
	import mx.controls.listClasses.IListItemRenderer;
	import mx.states.Transition;
	
	import spark.components.Image;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.effects.Scale;
	
	[Style(name="borderWeight",type="Number",inherit="no")]
	
	[Style(name="borderColor", type="uint", format="Color", theme="spark",inherit="no")]
	
	[Style(name="borderSkin",type="Class")]
	
	[SkinState("normal")]
	
	[SkinState("over")]
	
	[SkinState("disable")]
	
	public class CustomBorderImage extends SkinnableComponent implements IListItemRenderer
	{
		[SkinPart(required="true")]
		public var myBorder:SkinnableComponent;
		
		[SkinPart(required="true")]
		public var myImage:Image;
		
		private var _overTransition:Transition;
		
		private var _recTransition:Transition;
		
		private var _scaleOverEffect:Scale;
		
		private var _scaleRecEffect:Scale;
		
		
		public function CustomBorderImage()
		{
			super();
			setStyle("skinClass",CustomBorderImageSkin);
			setStyle("borderColor",0xcccccc);
			setStyle("borderWeight",5);
			addEventListener( MouseEvent.MOUSE_OVER,onMouseOver );
			addEventListener( MouseEvent.MOUSE_OUT,onMouseOut );
			createTranstion();
		}
		
		private var _borderSkin:Class;

		public function get borderSkin():Class
		{
			return _borderSkin;
		}

		public function set borderSkin(value:Class):void
		{
			_borderSkin = value;
			myBorder.setStyle("skinClass",value);
		}
		
		private var _data:Object;
		public function set data( value:Object ):void
		{
			_data = value;
			if( value.hasOwnProperty(sourceField))
			{
				imageSource = value[sourceField] as Object;
			}
		}
		
		public function get data():Object
		{
			return _data;
		}
		
		private var _sourceField:String;

		public function get sourceField():String
		{
			return _sourceField;
		}

		public function set sourceField(value:String):void
		{
			_sourceField = value;
		}
//		
//		private var _borderWeight:Number=5;
//
//		public function get borderWeight():Number
//		{
//			return _borderWeight;
//		}
//
//		public function set borderWeight(value:Number):void
//		{
//			if( value < 0 )
//				return;
//			if( _borderWeight != value )
//			{
//				_borderWeight = value;
//				invalidateDisplayList();
//			}
//		}
		
		private var _isOver:Boolean = false;

		public function get isOver():Boolean
		{
			return _isOver;
		}

		public function set isOver(value:Boolean):void
		{
			if( _isOver != value )
			{
				_isOver = value;
				invalidateSkinState();
			}
			
		}

		
		private var _imageSource:Object;

		public function get imageSource():Object
		{
			return _imageSource;
		}

		public function set imageSource(value:Object):void
		{
			if( _imageSource != value )
			{		
				_imageSource = value;
				invalidateProperties();
			}
		}
		
		override protected function commitProperties():void
		{
			super.commitProperties();
			if( myImage && imageSource )
			{
				myImage.source = imageSource;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
		}
		
		override protected function getCurrentSkinState():String
		{
			var returnState:String = "normal";
			if( !enabled )
				returnState = "disable";
			else if( isOver )
				returnState = "over";
			return returnState;
				
		}
		
		private function onMouseOver( event:MouseEvent ):void
		{
			isOver = true;
		}
		
		private function onMouseOut( event:MouseEvent ):void
		{
			isOver = false;
		}
		
		private function createTranstion():void
		{
			_scaleOverEffect = new Scale(this);
			_scaleOverEffect.scaleXFrom = 1;
			_scaleOverEffect.scaleXTo=1.4;
			_scaleOverEffect.scaleYFrom = 1;
			_scaleOverEffect.scaleYTo=1.4;
			
			_scaleRecEffect = new Scale(this);
			_scaleRecEffect.scaleXFrom = 1.4;
			_scaleRecEffect.scaleXTo = 1;
			_scaleRecEffect.scaleYFrom = 1.4;
			_scaleRecEffect.scaleYTo = 1;
			
			_overTransition = new Transition();
			_overTransition.fromState = "normal";
			_overTransition.toState = "over";
			_overTransition.effect = _scaleOverEffect;
			
			_recTransition = new Transition();
			_recTransition.fromState = "over";
			_recTransition.toState = "normal";
			_recTransition.effect = _scaleRecEffect;
			
			transitions = [_overTransition,_recTransition];
		}
	}
}